import {
  localData
} from '../../tools/localData';
import {
  checkTool
} from '../../tools/check';
import {
  nav2
} from '../../tools/nav2';
import {
  dsx
} from '../../tools/datax';

const processTag = '__processed__'
const mobileReg = new RegExp(checkTool.CommonRegex.mobile),
  orderIdReg = /订单([0-9a-z]+)/,
  msgNotifyReg = /您有(\d+)条客户消息/

export const clipboardTool = {
  getMobile(text) {
    if (mobileReg.test(text)) return RegExp.$1
    return null
  },
  getOrderId(text) {
    if (orderIdReg.test(text)) return RegExp.$1
    return null
  },
  getMsgNum(text) {
    if (msgNotifyReg.test(text)) return RegExp.$1
    return null
  },
  setProcessed(text) {
    localData.set(processTag, text)
  },
  isProcessed(text) {
    return localData.get(processTag) == text
  },
  act(key, mobile, msgNum, orderId) {
    if (key == 'mobile') {
      wx.cloud.callFunction({
        name: 'userCmd',
        data: {
          cmd: 'findUserByMobile',
          mobile,
          field: {
            _id: true
          }
        }
      }).then(res => {
        const user = res.result
        if (!user || !user._id) return ui.toast(false, '未找到用户')

        nav2.staffEdit('edit', user._id)
      })
    } else if (key == 'msg') {
      nav2.chatList()
    } else if (key == 'order') {
      nav2.orderList('manage', null, JSON.stringify({
        orderId
      }))
    }
  }
}